#pragma once

#include <Const.h>
#include <QList.h>

class GameData;

struct StMove
{
    int index;
    int score;
};

inline bool operator<(const StMove &left, const StMove &right)
{
    return left.score < right.score;
}

class AI
{
public:
    AI(GameData *_data);
    virtual ~AI(void);

    void GetBestPos(int &row, int &line, GB::CHESS_TYPE type);
    void SetSearchDepth(int depth);

private:
    // Alpha-Beta 
    int SearchAlphaBeta(int &index, GB::CHESS_TYPE type, int depth, int alpha, int beta);
    // Max-Min
    int SearchBestValue(int &index, GB::CHESS_TYPE type, int depth);
    // History Heuristic
    int SearchHistory(int &index, GB::CHESS_TYPE type, int depth, int alpha, int beta);
    void InitHistoryTable();
    bool ImmdiatelyWin(int &index, GB::CHESS_TYPE type);
    void CreateMoves(QList<StMove> &moves);

    bool Loss(int &index, GB::CHESS_TYPE type);
    GameData *data;

    int leafCount;
    int maxDepth;
    int historyTable[GB::MaxRow * GB::MaxLine];
    
    static const int SearchScope = 2;
    static const int MAX_VALUE = 100000000;
    static const int MIN_VALUE = -MAX_VALUE;
};

